#!/bin/sh
#---------------------------------------------
#   xdg-desktop-icon
#
#   Utility script to install desktop items on a Linux desktop.
#
#   Refer to the usage() function below for usage.
#
#   Copyright 2009-2010, Fathi Boudra <fabo@freedesktop.org>
#   Copyright 2009-2010, Rex Dieter <rdieter@fedoraproject.org>
#   Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
#   Copyright 2006, Jeremy White <jwhite@codeweavers.com>
#
#   LICENSE:
#
#---------------------------------------------

usage()
{
cat << '_USAGE'
_USAGE
}

manualpage()
{
cat << '_MANUALPAGE'
_MANUALPAGE
}

#@xdg-utils-common@

[ x"$1" != x"" ] || exit_failure_syntax

action=
desktop_file=

case $1 in
  install)
    action=install
    ;;

  uninstall)
    action=uninstall
    ;;

  *)
    exit_failure_syntax "unknown command '$1'"
    ;;
esac

shift

vendor=true
while [ $# -gt 0 ] ; do
    parm=$1
    shift

    case $parm in
      --novendor)
        vendor=false
        ;;

      -*)
        exit_failure_syntax "unexpected option '$parm'"
        ;;

      *)
        if [ -n "$desktop_file" ] ; then
            exit_failure_syntax "unexpected argument '$parm'"
        fi
        if [ "$action" = "install" ] ; then
            check_input_file "$parm"
        fi
        desktop_file=$parm
        ;;
    esac
done

# Shouldn't happen
if [ -z "$action" ] ; then
    exit_failure_syntax "command argument missing"
fi

if [ -z "$desktop_file" ] ; then
    exit_failure_syntax "FILE argument missing"
fi

filetype=
case "$desktop_file" in
  *.desktop)
     filetype=desktop
     if [ "$vendor" = "true" ] && [ "$action" = "install" ] ; then
        check_vendor_prefix "$desktop_file"
     fi
     ;;
  *)
     filetype=other
     ;;
esac

my_umask=077
desktop_dir="$HOME/Desktop"
if xdg-user-dir 2>/dev/null 1>&2; then
  desktop_dir=`xdg-user-dir DESKTOP`
fi
desktop_dir_kde=`kde${KDE_SESSION_VERSION}-config --userpath desktop 2> /dev/null`
if gconftool-2 -g /apps/nautilus/preferences/desktop_is_home_dir 2> /dev/null | grep true > /dev/null; then
    desktop_dir_gnome="$HOME"
    # Don't create $HOME/Desktop if it doesn't exist
    [ -w "$desktop_dir" ] || desktop_dir=
fi    
if [ -n "$desktop_dir_kde" ]; then
    if [ ! -d "$desktop_dir_kde" ]; then
        save_umask=`umask`
        umask $my_umask
        mkdir -p "$desktop_dir_kde"
        umask $save_umask
    fi
    # Is the KDE desktop dir != $HOME/Desktop ?
    if [ "x$(xdg_realpath "$desktop_dir")" != "x$(xdg_realpath "$desktop_dir_kde")" ]; then
        # If so, don't create $HOME/Desktop if it doesn't exist
        [ -w "$desktop_dir" ] || desktop_dir=
    else
        desktop_dir_kde=
    fi
fi

basefile=`basename "$desktop_file"`

DEBUG 1 "$action $desktop_file in $desktop_dir $desktop_dir_kde $desktop_dir_gnome"

case $action in
    install)
        save_umask=`umask`
        umask $my_umask

        for x in "$desktop_dir" "$desktop_dir_kde" "$desktop_dir_gnome" ; do
            if [ -n "$x" ]; then
                mkdir -p "$x"
                eval 'cp "$desktop_file" "$x/$basefile"'$xdg_redirect_output 
                chmod u+x "$x/$basefile"
            fi
        done

        umask $save_umask
        ;;

    uninstall)
        for x in "$desktop_dir" "$desktop_dir_kde" "$desktop_dir_gnome" ; do
            if [ -n "$x" ]; then
                rm -f "$x/$basefile"
            fi
        done

        ;;
esac

exit_success


